package com.bettem.modules.tourism.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bettem.common.utils.Constant;
import com.bettem.common.utils.PageUtils;
import com.bettem.common.utils.Query;
import com.bettem.common.utils.ShiroTokenUtils;
import com.bettem.modules.tourism.dao.TourismProductHomeImgDao;
import com.bettem.modules.tourism.dao.TourismProductPicDao;
import com.bettem.modules.tourism.entity.TourismProductHomeImgEntity;
import com.bettem.modules.tourism.entity.TourismProductPicEntity;
import com.bettem.modules.tourism.service.TourismProductHomeImgService;
import com.bettem.modules.tourism.service.TourismProductPicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("tourismProductHomeImgService")
public class TourismProductHomeImgServiceImpl extends ServiceImpl<TourismProductHomeImgDao, TourismProductHomeImgEntity> implements TourismProductHomeImgService {

    @Autowired
    private ShiroTokenUtils shiroTokenUtils;

    //图片地址
    @Value("${bettem.imagePath}")
    private String imagePath;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<TourismProductHomeImgEntity> page = this.selectPage(
                new Query<TourismProductHomeImgEntity>(params).getPage(),
                new EntityWrapper<TourismProductHomeImgEntity>().eq("delete_state",Constant.DELETE_STATE_NO)
                .orderBy("sort_num")
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(String[] ids) {
        List<TourismProductHomeImgEntity> tourismProductHomeImgList=new ArrayList<>();
        TourismProductHomeImgEntity tourismProductHomeImgEntity=null;
        for(int i=0;i<ids.length;i++){
            tourismProductHomeImgEntity=new TourismProductHomeImgEntity();
            tourismProductHomeImgEntity.setId(ids[i]);
            tourismProductHomeImgEntity.setModifyUserId(shiroTokenUtils.getUserId());
            tourismProductHomeImgEntity.setModifyDate(new Date());
            tourismProductHomeImgEntity.setDeleteState(Constant.DELETE_STATE_YES);
            tourismProductHomeImgList.add(tourismProductHomeImgEntity);
        }
        this.updateBatchById(tourismProductHomeImgList);
    }

    @Override
    public List<TourismProductHomeImgEntity> getHomeImgList(int count) {
        List<TourismProductHomeImgEntity> list=this.selectList(new EntityWrapper<TourismProductHomeImgEntity>()
                .eq("delete_state",Constant.DELETE_STATE_NO)
                .orderBy("sort_num")
                .last("limit " + count)
        );
        for(TourismProductHomeImgEntity tourismProductHomeImg:list){
            tourismProductHomeImg.setImgUrl(imagePath+tourismProductHomeImg.getImgUrl());
        }
        return list;
    }

}
